# 读写文件

# 读文件
f1 = open("../data/students.txt", mode='r', encoding='utf8')

# 读取n个字符
print(f1.read(10))
# 读取一行数据
print(f1.readline())
# 将所有行以list的形式返回
print(f1.readlines())
# f1 的mode为r 表示读文件 不能写文件
# f1.write("1111")

# 关闭文件
f1.close()

# 写文件
# mode = w 默认是 overwrite
f2 = open("../data/f2.txt", mode='w', encoding='utf8')

f2.write("abc\n")
f2.write("efg\n")
f2.write("hijk\n")

f3 = open("../data/f3.txt", mode='a', encoding='utf8')

f3.write("abc\n")
f3.write("efg\n")
f3.write("hijk\n")

# list1 = [1, 2, 3, 4, 5]
# 在使用writelines时要注意 写入的每个元素必须是字符串
# f3.writelines(list1)

# 关闭文件
f2.close()

# with open 方式(推荐)
# 自动关闭文件

# stu_f = open("../data/students.txt",mode='r',encoding='utf8')
# stu_fw = open("../data/students_new.txt",mode='w',encoding='utf8')
#
# stu_fw.writelines(stu_f.readlines())

with open("../data/students.txt", mode='r', encoding='utf8') as stu_f:
    with open("../data/students_new.txt", mode='w', encoding='utf8') as stu_fw:
        stu_fw.writelines(stu_f.readlines())

# Python中str不能和int直接相加
# print("abc" + 123)
print("abc" + str(123))
